package com.yanqu.road.server.cmd.mogulbirthday;

import com.yanqu.road.dao.impl.config.DecorationDaoImpl;
import com.yanqu.road.dao.impl.player.UserDecorationDaoImpl;
import com.yanqu.road.entity.config.player.UserDecorationInfoResult;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserDecoration;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.bossbirthday.MogulBirthdayProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.manger.DecorationMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_MOGUL_BIRTHDAY_BEST_DECORATION_FETCH, desc = "上传最高称号")
public class MogulUploadBestDecorationCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        MogulBirthdayProto.MogulBirthdayUploadBestDecorationReqMsg reqMsg = MogulBirthdayProto.MogulBirthdayUploadBestDecorationReqMsg.parseFrom(yanQuMessage.getBytes());

        MogulBirthdayProto.MogulBirthdayUploadBestDecorationRespMsg.Builder builder = MogulBirthdayProto.MogulBirthdayUploadBestDecorationRespMsg.newBuilder();
        long nowTime = DateHelper.getCurrentSecond();
        for (long userId : reqMsg.getUserIdList()) {
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if(userInfo == null){
                continue;
            }
            MogulBirthdayProto.MogulBirthdayBestTitleTemp.Builder userBuilder = MogulBirthdayProto.MogulBirthdayBestTitleTemp.newBuilder();
            userBuilder.setBaseInfo(PlayerPb.parsePlayerBaseTempMsg(userInfo));
            userBuilder.setUserId(userId);
            //最高称号
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
            if(player != null){
                UserDecorationInfoResult result = player.getModule(DecorationModule.class).getCurMaxDecorationInfo();
                userBuilder.setDecorationId(result.decorationId);
            }else {
                //
                Map<Integer, UserDecoration> decorationMap = new UserDecorationDaoImpl().getUserDecorationMap(userId);
                List<Integer> decorationIdList = StringUtils.stringToIntegerList(userInfo.getDecoration(), DecorationMgr.SPLIT_SEPARATOR);
                for (Map.Entry<Integer, UserDecoration> entry : decorationMap.entrySet()) {
                    if(entry.getValue().getEndTime() > 0 && entry.getValue().getEndTime() < nowTime){
                        continue;
                    }
                    if(!decorationIdList.contains(entry.getKey())){
                        decorationIdList.add(entry.getKey());
                    }
                }
                //最高称号ID
                int decorationId = GoodsMgr.getUserDecorationMaxLevel(decorationIdList);
                userBuilder.setDecorationId(decorationId);
            }

            builder.addBest(userBuilder);
        }
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_MOGUL_BIRTHDAY_BEST_DECORATION_UPLOAD, builder));
    }
}
